﻿/*
This is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this software; if not, write to the Free
Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
/*
DNJ Plugin script template 
*/


/*[DNJTPL::HEADER]*/
function DNJDebug(msg)
{   
    if (window.console && window.console.warn) console.warn("DNJ DBG :: %s", msg);
    else alert(msg);
}

(function (jQuery){


/* This part of code is inspired from jquery.namespace plugin by Gilberto Saraiva (saraivagilberto@gmail.com 
 * Contributors:
 *    Marc van Neerven (marc@smartsite.nl) - Javascript .apply structure (function.this = current filter)
 * Support/Site: http://gsaraiva.projects.pro.br/openprj/?page=jquerynamespace
 *
 * Modifications for DNJ project by Alaa-eddine KADDOURI : 
	- Removed almost all evals, only one eval expression is now generated per namespace declaration
	- Removed namespaceData variable
	- Added JQuery functions namespacing  , 
	    this feature was added tomake use of DNJRPC more natural so you can call a function as a jquery plugin or as a jquery extension
	
	- [Fix]
 */

   
    
  jQuery.fn.extend({ 
	curReturn:null,
	jQueryInit: jQuery.fn.init,
  	init: function( selector, context ) {	
	      jQuery.fn.curReturn = new jQuery.fn.jQueryInit(selector, context);
	      return jQuery.fn.curReturn;
  	}
    });

    function NSFactory(namespaces, objects){	
      if(typeof objects == "function"){
        if(namespaces.match(".")){
          nss = namespaces.split(".");
          snss = "";
	      _fn = jQuery.fn;
	      o=jQuery;
          for(var i = 0; i < nss.length; i++){		
            _fn[nss[i]]= _fn[nss[i]] || {};   
            _fn=_fn[nss[i]];

            o[nss[i]]= o[nss[i]] || {};   
            o=o[nss[i]];

            snss += "['" + nss[i] + "']";
          }	  
	      _function = function(){return objects.apply(jQuery.fn.curReturn, arguments); };
          eval("jQuery" + snss +" = objects;jQuery.fn" + snss +" = _function");
        }else{
          jQuery.extend({
            namespaces: function(){
              return objects(jQuery.fn.curReturn);
            }
          });
        }
      }else{
        for(var space in objects){
          NSFactory(namespaces + "." + space, objects[space]);
        };
      }
    }



	jQuery.DNJRPC = function(options){
	var settings = 
	    {
	        success: null
	    };
	var EObj = this;    
	var jsoncount = 1;
	var jsonQ = new Array();
	if(options) { $.extend(settings, options);}
    function SuccessCB(data){
    
            if (typeof(settings.success) == 'function') 
                settings.success(data);

            var target = jsonQ[data.id];
            if(typeof(target.val)== 'function')
            {
                target.val(data.result);
            }
            else            
            if(typeof(target.html)== 'function')
            {
                target.html(data.result);
                
            }   
            jsonQ[data.id] = null;
    };

    
    function DNJCall(function_name, _arguments, target)
    { 
        var jsondata = {method:function_name, params:new Array(), id:jsoncount};
        jsonQ[jsoncount] = target;
        jsoncount++;
        var args='';        
        for (var i = 0; i < _arguments.length; i++)
            jsondata.params.push(_arguments[i]);
               
        $.post('DNJUndefinedInvoker', jsondata, SuccessCB, 'json');
                    
    };
/*[/DNJTPL::HEADER]*/

/*[DNJTPL::LOOP]*/    
NSFactory('DNJUndefinedPackage', {DNJUndefinedSub: function(){DNJCall('DNJUndefinedSub', arguments, this);}});
/*[/DNJTPL::LOOP]*/


/*[DNJTPL::MAPPING_ERROR]*/    
	DNJDebug("unable to load mapping file : DNJMAPPING");
/*[/DNJTPL::MAPPING_ERROR]*/

            
/*[DNJTPL::FOOTER]*/
    return jQuery;}
})(jQuery);
/*[/DNJTPL::FOOTER]*/




/**detects missuse of this script template**/
(function (jQuery){
	jQuery.DNJRPC = function(options){	
	DNJDebug("You are trying to use DNJTPL.js as a jquery plugin!! Please don't call this script directly, use the DNJ Http handler instead!");
    return null;}
})(jQuery);